package test.lemonyfei.http.utils;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer;

import javax.net.ssl.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.KeyStore;
import java.util.concurrent.Executors;

/**
 * Created by sh00556 on 2017/4/25.
 */
public class HttpMockServer {
    private static final String pathOfkeyFile = "testkey.jks";
    private HttpServer server;

    public void initServer(int port) {
        InetSocketAddress addr = new InetSocketAddress(port);
        try {
            this.server = HttpServer.create(addr, 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initServer(int port, String protocolType) {
        InetSocketAddress addr = new InetSocketAddress(port);
        try {
            this.server = HttpsServer.create(addr, 0);
            SSLContext sslContext = SSLContext.getInstance(protocolType);

            char[] password = "password".toCharArray();
            KeyStore ks = KeyStore.getInstance("JKS");
            FileInputStream fis = new FileInputStream("testkey.jks");
            ks.load(fis, password);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, password);

            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(ks);

            sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            ((HttpsServer) this.server).setHttpsConfigurator(new HttpsConfigurator(sslContext) {
                public void configure(HttpsParameters params) {
                    try {
                        SSLContext c = SSLContext.getDefault();
                        SSLEngine engine = c.createSSLEngine();
                        params.setNeedClientAuth(false);
                        params.setCipherSuites(engine.getEnabledCipherSuites());
                        params.setProtocols(engine.getEnabledProtocols());
                        SSLParameters defaultSSLParameters = c.getDefaultSSLParameters();
                        params.setSSLParameters(defaultSSLParameters);
                    } catch (Exception ex) {
                        System.out.println("Failed to create HTTPS port");
                    }
                }
            });
        } catch (Exception exception) {
            System.out.println("Failed to create HTTPS server on port 8000 of localhost");
            exception.printStackTrace();
        }
    }

    public void createContext(HttpContext httpContext) {
        HttpHandle httpHandle = new HttpHandle(httpContext);
        this.server.createContext(httpContext.getUrl(), httpHandle);
    }

    public void startServer() {
        this.server.setExecutor(Executors.newCachedThreadPool());
        this.server.start();
    }

    public void stop() {
        this.server.stop(0);
    }
}
